/**
 * Description:一些注释<br/>
 * CreateTime:2019-04-16<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
package cn.cnsy123.bookwebsiteserver.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.cnsy123.bookwebsiteserver.vo.layui.CommonResult;

/**
 * Description:一些注释<br/>
 * CreateTime:2019-04-16<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
@ControllerAdvice
public class ParamExceptionHandler {
    
    private static final Logger logger = LoggerFactory.getLogger(ParamExceptionHandler.class);
    
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public CommonResult<Object> paramErrorExceptionHandler(MethodArgumentNotValidException exception) {
        FieldError fieldError = exception.getBindingResult().getFieldError();
        logger.info("参数错误信息:{}",fieldError);
        CommonResult<Object> commonResult = new CommonResult<Object>();
        commonResult.setCode(1);
        commonResult.setMsg(fieldError.getDefaultMessage());
        return commonResult;
    }
    
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public CommonResult<Object> paramErrorExceptionHandler(BindException exception) {
        FieldError fieldError = exception.getBindingResult().getFieldError();
        logger.info("参数错误信息:{}",fieldError);
        CommonResult<Object> commonResult = new CommonResult<Object>();
        commonResult.setCode(1);
        commonResult.setMsg(fieldError.getDefaultMessage());
        return commonResult;
    }

}
